package com.dm.scheduled;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.Filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.alibaba.fastjson.JSON;
import com.dm.controller.SysUserController;
import com.dm.dao.DataInfoDAO;
import com.dm.dao.SysUserDAO;
import com.dm.entity.CompanyInfoDO;
import com.dm.entity.DataInfoDO;
import com.dm.entity.SysUserDO;
import com.dm.filter.SessionFilter;
import com.dm.query.QueryCompanyInfo;
import com.dm.service.CompanyInfoService;
import com.dm.utils.HttpClient;
import com.dm.utils.SysInfoUtil;
import com.fasterxml.jackson.annotation.JsonAlias;


@Component
public class SpringScheduled {
	
	@Resource
	DataInfoDAO dataInfoDAO ; 
	
	@Resource
	private SysUserDAO sysUserDAO;

	
	@Autowired
	CompanyInfoService companyInfoService ;
	
	//@Value("${openurl}")
    private String url = "http://47.110.54.212:5001/";
	
	//重置访问人数
    //@Scheduled(cron = "0 */1 * * *  ?")
	@Scheduled(cron = "0 0 0 * * ?")
    public void acceScheduled(){
		System.out.println("重置访问人数");
		SysInfoUtil.loginInfo = new HashMap<String, Integer>();
		SysInfoUtil.userNumber = 0 ;
	}
	
	//告警推送
	@Scheduled(cron = "0 */1 * * *  ?")
	//@Scheduled(cron = "0 0 0 * * ?")
	public void pushScheduled(){
		System.out.println("推送");
		
		List< CompanyInfoDO >   companyData = companyInfoService.selectCompanyInfoList(new QueryCompanyInfo());
		Map<String, CompanyInfoDO> companyMap = new HashMap<>();
		for(CompanyInfoDO company : companyData){
			companyMap.put(company.getCompanyCode(), company);
		}
		
		List<DataInfoDO> data = dataInfoDAO.selectDataInfoBystatus(0);
		System.out.println("更新个数："+data.size());
	    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String time = sdf.format(new Date());
		for(DataInfoDO dataInfo : data){
			boolean push = false ; 
			MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
			CompanyInfoDO companyInfo = companyMap.get(dataInfo.getCcode());
			if(companyInfo!=null){
				if(companyInfo.getNonTotalHydrocarbonWarning()!=null){
					Float fzw = Float.valueOf(dataInfo.getV26val());
					Float fzwWaring = Float.valueOf(companyInfo.getNonTotalHydrocarbonWarning());
					List<String> str = new ArrayList<String>();
					//非甲烷总烃累计排放(kg)-当年 超标
					if(fzw > fzwWaring){
						push = true; 
						//查询该企业的人员 
						List<SysUserDO> sysdata = sysUserDAO.selectSysUserListByCode(companyInfo.getCompanyCode(),companyInfo.getGroupCode());
						for(SysUserDO sysUserDO : sysdata){
							Map pushData = new HashMap<>();
							Map info = new HashMap<>();
							info.put("company", companyInfo.getCompanyName());
							info.put("ass", "非甲烷总烃");
							info.put("time", time);
							pushData.put("openId", sysUserDO.getWxid());
							pushData.put("info", info);
							str.add(JSON.toJSONString(pushData));
						}
						params.add("data", "["+String.join(",", str)+"]");
						HttpClient.sendPostRequest(url+"notify", params);
					}
				}
				//总烃
//				if(companyInfo.getTotalHydrocarbonWarning()!=null){
//					Float fzw = Float.valueOf(dataInfo.getV23val());
//					Float fzwWaring = Float.valueOf(companyInfo.getTotalHydrocarbonWarning());
//					if(fzw > fzwWaring){
//						//查询该企业的人员 
//						List<SysUserDO> sysdata = sysUserDAO.selectSysUserListByCode(companyInfo.getCompanyCode(),companyInfo.getGroupCode());
//						for(SysUserDO sysUserDO : sysdata){
//							Map<String,String> pushData = new HashMap<>();
//							Map<String,String> info = new HashMap<>();
//							info.put("company", companyInfo.getCompanyName());
//							info.put("ass", "总烃");
//							info.put("time", time);
//							pushData.put("openId", sysUserDO.getWxid());
//							pushData.put("info", JSON.toJSONString(info));
//							params.add("data", JSON.toJSONString(pushData));
//						}
//						HttpClient.sendPostRequest(url+"notify", params);
//					}
//
//				}
				//苯系物
				if(companyInfo.getBenzeneWarning()!=null){
					push = true; 
					Float b1 = Float.valueOf(dataInfo.getV27val());
					Float b2 = Float.valueOf(dataInfo.getV28val());
					Float b3 = Float.valueOf(dataInfo.getV29val());
					Float fzwWaring = Float.valueOf(companyInfo.getBenzeneWarning());
					StringBuffer sb = new StringBuffer();
					List<String> str = new ArrayList<String>();
					if((b1+b2+b3) > fzwWaring){
						//查询该企业的人员 
						List<SysUserDO> sysdata = sysUserDAO.selectSysUserListByCode(companyInfo.getCompanyCode(),companyInfo.getGroupCode());
						for(SysUserDO sysUserDO : sysdata){
							Map pushData = new HashMap<>();
							Map info = new HashMap<>();
							info.put("company", companyInfo.getCompanyName());
							info.put("ass", "苯系物");
							info.put("time", time);
							pushData.put("openId", sysUserDO.getWxid());
							pushData.put("info", info);
							str.add(JSON.toJSONString(pushData));
						}
						params.add("data", "["+String.join(",", str)+"]");
						HttpClient.sendPostRequest(url+"notify", params);
					}
				}
				if(push){
					dataInfoDAO.insertDataInfo(dataInfo);
				}
			}
		}
		dataInfoDAO.updateDataInfo();
	}

}
